package event.money.tracker;


import MTClient.ClientLogic;
import MTClient.ClientTCPConnection;
import MTClient.MTConstant;
import MTRequest.MTRequestDataSource;
import SQLManager.SQLManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MoneyTrackerActivity extends Activity {
    /** Called when the activity is first created. */
	
	ClientTCPConnection mClientTCP = null;
	int mRequestID = 0;
	
	private ProgressDialog loadingpopup = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
		ClientLogic.GetInstance().init(this.getApplicationContext());
		
		// set current activity (need this for callback)
        ClientLogic.SetCurrentActivity(this);
        
	    if(ClientLogic.GetInstance().pref_check_user_known()){
	    	// skip welcome register screen if user is known
	    	Intent intent = new Intent(this, MainTabView.class);
	    	startActivity(intent);
	    	finish();
	    	return;
	    }
        
        setContentView(R.layout.main);
        
        // get phone number and update edit field
        String phone_nr = ClientLogic.GetInstance().get_phone_number();
        ((EditText)findViewById(R.id.phonenr_input)).setText(phone_nr);
        
    }
    
    public void register_btn_click(View register_button) {
    	
    	// popup loading dialog
    	loadingpopup = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
    	
    	// do logic (networking!)
    	mRequestID = MTConstant.MT_REQID_REGISTER;
    	ClientLogic.GetInstance().SendRegisterRequest(
				((EditText)findViewById(R.id.phonenr_input)).getText().toString(),
				((EditText)findViewById(R.id.name_input)).getText().toString(),
				this );
    	
    	
    }
    
    public void handle_register_result(boolean success) {
    	
    	loadingpopup.dismiss();
    	
    	if(success) {
    		
    		// popup success for a few seconds, then go on to main tab view
    		
	        this.runOnUiThread(new Runnable() {
    		    public void run() {
    		    	Toast.makeText(MoneyTrackerActivity.this.getApplicationContext(), "Registration successful!",
    			            Toast.LENGTH_SHORT).show();
    		    }
    		});
	        
    		Intent intent = new Intent( this.getApplicationContext() , MainTabView.class);
    		startActivity(intent);
    		
    		finish();
    		
    	} else {
    		
    		this.runOnUiThread(new Runnable() {
    		    public void run() {
    		    	Toast.makeText(MoneyTrackerActivity.this.getApplicationContext(), "Registration failed! Please register a unique telephone number.",
    	    	            Toast.LENGTH_LONG).show(); 
    		    }
    		});
    			
    	}
    	
    }
    

	
}